'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    ------
    
    About:
'''
import maya.OpenMaya as OpenMaya

import masdevallia.utility.Dag as Dag

class BaseMeta( object ):
    def __init__( self, node_name=None, node_type='BaseMeta' ):
        '''
        @param node_name: String. Name of the node.
        '''
        self.version = 1.0
        self.type = node_type
        
        # Get or make the node.
        if Dag.objExists( node_name ):
            obj = Dag.getMObject( node_name )
            if obj.apiType() == OpenMaya.MFn.kAffect:
                self.mobject = obj
                self.name = node_name
        else:
            self.mobject = Dag.createNode( inNodeName=node_name, inNodeType='network' )
            self.name = node_name
            
            # Add the nodes attributes.
            self.initAttributes()
            
    def initAttributes( self ):
        '''
        Default attributes for this node.
        '''
        dep_fn = OpenMaya.MFnDependencyNode()
        dep_fn.setObject( self.mobject )
        
        # Version.
        self.addAttribute( 'float', 'version', 'version', default=self.version )
        
        # Name.
        self.addAttribute( 'string', 'metaName', 'metaName' )
        self.setAttributeValue( 'metaName', self.name )
        
        # Type.
        self.addAttribute( 'string', 'metaType', 'metaType' )
        self.setAttributeValue( 'metaType', self.type )
        
        # Module Type.
        self.addAttribute( 'string', 'moduleType', 'moduleType' )
        
        # Module Name.        
        self.addAttribute( 'string', 'moduleName', 'moduleName' )
        
        # Module Class.
        self.addAttribute( 'string', 'moduleClass', 'moduleClass' )

    def addAttribute( self, attr_type, long_name, short_name, default=None ):
        '''
        Adds an attribute to the meta node.
        '''
        dep_fn = OpenMaya.MFnDependencyNode()
        dep_fn.setObject( self.mobject )
        
        # Numeric: Float.
        if attr_type == 'float':
            attr = OpenMaya.MFnNumericAttribute()
            obj = attr.create( long_name, short_name, OpenMaya.MFnNumericData.kFloat )
            attr.setHidden( False )
                        
            if default is not None:
                attr.setDefault( self.version )
            
        # Typed: String.
        if attr_type == 'string':
            attr = OpenMaya.MFnTypedAttribute()
            obj = attr.create( long_name, short_name, OpenMaya.MFnData.kString )
            attr.setHidden( False )
        
        # Add the attribute to the node    
        dep_fn.addAttribute( obj )        
        
    def setAttributeValue( self, attr_name, new_value ):
        '''
        Sets the attribute to the desired value.
        
        @param attr_name: String.
        @param new_value: Type is MPlug dependent.
        '''
        plug = Dag.getPlugByName( self.mobject, attr_name )
        Dag.setPlugValue( plug, new_value )
        
    def getAttributeValue( self, attr_name ):
        '''
        Gets the attributes value.
        
        @param attr_name: String. Attr name.
        @return: attr value.
        '''
        plug = Dag.getPlugByName( self.mobject, attr_name )
        return Dag.getPlugValue( plug )
    
    def getVersion( self ):
        return self.getAttributeValue( 'version' )
    
    def getMetaName( self ):
        return self.getAttributeValue( 'metaName' )
    
    def getMetaType( self ):
        return self.getAttributeValue( 'metaType' )
    
    def getModuleType( self ):
        return self.getAttributeValue( 'moduleType' )
    
    def getModuleName( self ):
        return self.getAttributeValue( 'moduleName' )
    
    def getModuleClass( self ):
        return self.getAttributeValue( 'moduleClass' )